package cn.bjc.order.controller;

import cn.bjc.basic.util.JsonResult;
import cn.bjc.basic.util.LoginContext;
import cn.bjc.basic.util.PageList;
import cn.bjc.order.domain.OrderPetAcquisition;
import cn.bjc.order.dto.OrderDto;
import cn.bjc.order.query.OrderPetAcquisitionQuery;
import cn.bjc.order.service.IOrderPetAcquisitionService;

import cn.bjc.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//@Controller
//@ResponseBody
@RestController //controller层,返回json数据
@RequestMapping("/orderPetAcquisition")
//接口文档的注解
@Api(value = "订单的API",description="订单相关的CRUD功能")
public class OrderPetAcquisitionController {

    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;

    /**
     *  查询所有:
     *  动词:get
     * @return
     */
    //@RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping()//上面的结合
    @ApiOperation(value = "查询所有")
    public List<OrderPetAcquisition> findAll(HttpServletRequest request) {
        //获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);

        return orderPetAcquisitionService.loadAll();
    }


    /**
     * 查询一个:接收参数id
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询")
    public OrderPetAcquisition findOne(@PathVariable("id") long id) {
        return orderPetAcquisitionService.loadById(id);
    }
    
    /**
     * 分页查询
     * 
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query,HttpServletRequest request){
        try {

            Logininfo logininfo = LoginContext.getLogininfo(request);
            return orderPetAcquisitionService.pageQuery(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<OrderPetAcquisition>();
        }
    }
    /**
     * 确认订单
     */
    @PostMapping("/confirm")
    @ApiOperation(value = "确认订单接口")
    public JsonResult confirm(@RequestBody OrderDto dto){
        try {
            orderPetAcquisitionService.confirm(dto);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("订单确认失败,稍后重试");
        }
    }

    /**
     * 取消订单
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "取消订单接口")
    public JsonResult cancel(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.cancel(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("订单取消失败,稍后重试");
        }
    }
}
